package web.controller;

import hibernate.pojo.Voting;
import hibernate.pojo_address_telefon.TelephoneTypes;
import hibernate.util.HibernateUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.faces.model.SelectItem;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;

public class CreateNewVotingRecordController {

	private static final Log logger = LogFactory.getLog(CreateNewVotingRecordController.class);

	private String votingName;
	private String detailInfo;
	private String numberOfVotingSelections;
	private String votingSelectionOne;
	private String votingSelectionTwo;
	private String votingSelectionThree;
	private String votingSelectionFour;
	private String votingSelectionFive;
	
	private static final String[] WEEK_DAY_LABELS = new String[] { "Sun *",
		"Mon +", "Tue +", "Wed +", "Thu +", "Fri +", "Sat *" };
	
	private Date selectedStartDate;
	private Date selectedEndDate;
	private boolean popup;
	private Locale locale;
	private String pattern;
	private boolean useCustomDayLabels;
	private boolean showApplyButton;
	
	private Voting voting;

	
//	private String areaCode;
//	private String phoneNumber;
//	private String name;
//	private TelephoneBook telephoneBook;
//	private TelephoneTypes telephoneTypes;
//	private Long listelemeSelectedTelephoneTypeId = new Long(0);
//	private List<SelectItem> telephoneTypesItemsList;
//	private List<TelephoneTypes> telephoneTypesList;

	public CreateNewVotingRecordController() {
		locale = Locale.US;
		popup = true;
		pattern = "dd/MM/yyyy";
	}

	public boolean validateTelephoneNumber() {

		return true;
	}

	public String fromMainUserPage(){
		
		String pageToRedirect = "";
		
		try {
			
			pageToRedirect = "fromMainUserPageSuccess";
				
		} catch (Exception exception) {
			logger.error("Error at Cancel Insert New Voting Record", exception);
		}
		
		return pageToRedirect;
	}
	
	public String insertNewVotingRecord() {

		String pageToRedirect = "";
		
//		if (!validateVotingInformation()) {
//			pageToRedirect = "validationErrorPage";
//			return pageToRedirect;
//		}
//		
//		int numberOfVotes = 0;
//		if(getNumberOfVotingSelections() != null && !getNumberOfVotingSelections().equals("")){
//			numberOfVotes = Integer.parseInt(getNumberOfVotingSelections());
//		}
//		else {
//			logger.error("Enter number of poll selections!");
//		}
		try {
			
//			if( numberOfVotes > 0){
//				
////				List<VotingSelections> votingSelections = new ArrayList<VotingSelections>();
////				//create voting selection objects
////				for (int i = 0; i < numberOfVotes; i++) {
////					VotingSelections selections = new VotingSelections();
////					selections.setSelectionName(getVotingSelectionOne());
////					selections.setVoting(getVoting());
////					votingSelections.add(selections);
////				}
//				
//				voting = new Voting();
//				voting.setVotingName(getVotingName());
//				voting.setStartDate(getSelectedStartDate());
//				voting.setEndDate(getSelectedEndDate());
//				voting.setDetailInfo(getDetailInfo());
//				voting.setNumberOfSelections(numberOfVotes);
//				voting.setVotingSelectionOne(getVotingSelectionOne());
//				voting.setVotingSelectionTwo(getVotingSelectionTwo());
//				voting.setVotingSelectionThree(getVotingSelectionThree());
//				voting.setVotingSelectionFour(getVotingSelectionFour());
//				voting.setVotingSelectionFive(getVotingSelectionFive());
//				
//				//voting.setVotingSelectionList(votingSelections);
//				Session session = HibernateUtil.getSessionFactory().openSession();
//				Transaction transaction = session.beginTransaction();
//				session.save(voting);
//				transaction.commit();
//				clearFields();
				pageToRedirect = "createdNewVotingRecordSuccess";
				
				
//			} else {
//				pageToRedirect = "validationErrorPage";
//				return pageToRedirect;
//			}
			
		} catch (Exception exception) {
			logger.error("New Voting Record Insert Error", exception);
		}

		return pageToRedirect;
	}
	
	private void clearFields() {
		votingName = "";
		selectedStartDate = null;
		selectedEndDate = null;
		detailInfo = "";
		numberOfVotingSelections = null;
		votingSelectionOne = "";
		votingSelectionTwo = "";
		votingSelectionThree = "";
		votingSelectionFour = "";
		votingSelectionFive = "";
	}

	public boolean validateVotingInformation(){
	
		boolean validationResult = true;
		
		
		
		return validationResult;
		
	}
	
	public String cancelInsertNewVotingRecord() {
		
		String pageToRedirect ="displayListOfAllVotings";
		
		try {
			
		} catch (Exception exception) {
			logger.error("Error at Cancel Insert New Voting Record", exception);
		}
		
		return pageToRedirect;
		
	}

	public static List<SelectItem> telephoneTypesListToSelectItemList(
			List<TelephoneTypes> telephoneTypesList) {

		List<SelectItem> returnTelephoneTypesSelectItemList = new ArrayList<SelectItem>();
		returnTelephoneTypesSelectItemList.add(new SelectItem(new Long(0),
				"Seçiniz"));
		if (telephoneTypesList != null && telephoneTypesList.size() > 0) {
			for (TelephoneTypes telephoneTypes : telephoneTypesList) {
				SelectItem selectItem = new SelectItem(telephoneTypes.getId(),
						telephoneTypes.getDescription());
				returnTelephoneTypesSelectItemList.add(selectItem);
			}
		}
		return returnTelephoneTypesSelectItemList;
	}

	public String getVotingName() {
		return votingName;
	}

	public void setVotingName(String votingName) {
		this.votingName = votingName;
	}

	public String getDetailInfo() {
		return detailInfo;
	}

	public void setDetailInfo(String detailInfo) {
		this.detailInfo = detailInfo;
	}

	public Voting getVoting() {
		return voting;
	}

	public void setVoting(Voting voting) {
		this.voting = voting;
	}

	public String getNumberOfVotingSelections() {
		return numberOfVotingSelections;
	}

	public void setNumberOfVotingSelections(String numberOfVotingSelections) {
		this.numberOfVotingSelections = numberOfVotingSelections;
	}

	public String getVotingSelectionOne() {
		return votingSelectionOne;
	}

	public void setVotingSelectionOne(String votingSelectionOne) {
		this.votingSelectionOne = votingSelectionOne;
	}

	public String getVotingSelectionTwo() {
		return votingSelectionTwo;
	}

	public void setVotingSelectionTwo(String votingSelectionTwo) {
		this.votingSelectionTwo = votingSelectionTwo;
	}

	public String getVotingSelectionThree() {
		return votingSelectionThree;
	}

	public void setVotingSelectionThree(String votingSelectionThree) {
		this.votingSelectionThree = votingSelectionThree;
	}

	public void setVotingSelectionFour(String votingSelectionFour) {
		this.votingSelectionFour = votingSelectionFour;
	}

	public String getVotingSelectionFour() {
		return votingSelectionFour;
	}

	public void setVotingSelectionFive(String votingSelectionFive) {
		this.votingSelectionFive = votingSelectionFive;
	}

	public String getVotingSelectionFive() {
		return votingSelectionFive;
	}

	public boolean isPopup() {
		return popup;
	}

	public void setPopup(boolean popup) {
		this.popup = popup;
	}

	public Locale getLocale() {
		return locale;
	}

	public void setLocale(Locale locale) {
		this.locale = locale;
	}

	public String getPattern() {
		return pattern;
	}

	public void setPattern(String pattern) {
		this.pattern = pattern;
	}
	
	public Object getWeekDayLabelsShort() {
		if (isUseCustomDayLabels()) {
			return WEEK_DAY_LABELS;
		} else {
			return null;
		}
	}

	public void setUseCustomDayLabels(boolean useCustomDayLabels) {
		this.useCustomDayLabels = useCustomDayLabels;
	}

	public boolean isUseCustomDayLabels() {
		return useCustomDayLabels;
	}

	public Date getSelectedStartDate() {
		return selectedStartDate;
	}

	public void setSelectedStartDate(Date selectedStartDate) {
		this.selectedStartDate = selectedStartDate;
	}

	public Date getSelectedEndDate() {
		return selectedEndDate;
	}

	public void setSelectedEndDate(Date selectedEndDate) {
		this.selectedEndDate = selectedEndDate;
	}

	public void setShowApplyButton(boolean showApplyButton) {
		this.showApplyButton = showApplyButton;
	}

	public boolean isShowApplyButton() {
		return showApplyButton;
	}

}
